﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Linq;
using System.Xml;
using PSE.Tools.WasConfigGenerator;
using System.Diagnostics;
using System.Threading;

namespace PSE.Tools.WasConfigGeneratorConsole
{
    class Program
    {
        static StringBuilder _eventLog = new StringBuilder();

        static int Main(string[] args)
        {
            string serviceAssembliesPath = null;

            ShowInformation("Was Config Generator BM&FBovespa\n");

            if (args.Length != 1)
            {
                ShowInformation(@"Usage: WasConfigGenerator.exe [/sa ServiceAssembliesPath]");
                ShowInformation(@"/sa ServiceAssembliesPath: Root path of service assemblies");
                return -1;
            }
            else
            {
                serviceAssembliesPath = args[0];
            }


            if (!string.IsNullOrEmpty(serviceAssembliesPath))
            {
                ServiceAssembliesConfigGenerator assembliesConfigGenerator = ServiceAssembliesConfigGenerator.GetInstance();
                assembliesConfigGenerator.Status += new EventHandler<StatusEventArgs>(configGenerator_Status);
                assembliesConfigGenerator.Error += new BaseConfigGenerator.ErrorEventHandler(assembliesConfigGenerator_Error);

                try
                {
                    assembliesConfigGenerator.Generate(serviceAssembliesPath);
                }
                catch (Exception ex)
                {
                    ShowError(ex);
                    return -1;
                }
                finally
                {
                    EventLog.WriteEntry("Was Config Generator", _eventLog.ToString());
                }
            }

            return 0;
        }

        static void assembliesConfigGenerator_Error(object sender, PSE.Tools.WasConfigGenerator.ErrorEventArgs e)
        {
            ShowError(e.Exception);
        }

        static void configGenerator_Status(object sender, StatusEventArgs e)
        {
            switch (e.Type)
            {
                case StatusType.Information:
                    ShowInformation(e.Message);
                    break;
                case StatusType.Step:
                    ShowStep(e.Message);
                    break;
                case StatusType.Success:
                    ShowSuccess(e.Message);
                    break;
                default:
                    ShowInformation(e.Message);
                    break;
            }
        }

        private static void ShowInformation(string message, params object[] args)
        {
            Console.WriteLine(message, args);
            _eventLog.AppendLine(string.Format(message, args));
        }
        private static void ShowStep(string message, params object[] args)
        {
            Console.ForegroundColor = ConsoleColor.Cyan;

            Console.WriteLine(message, args);
            _eventLog.AppendLine(string.Format(message, args));

            Console.ForegroundColor = ConsoleColor.Gray;
        }
        private static void ShowSuccess(string message, params object[] args)
        {
            Console.ForegroundColor = ConsoleColor.Green;

            Console.WriteLine(message, args);
            _eventLog.AppendLine(string.Format(message, args));

            Console.ForegroundColor = ConsoleColor.Gray;
        }

        private static void ShowError(Exception ex)
        {
            Console.ForegroundColor = ConsoleColor.Red;

            string error = "There was an error generating configuration:\n" + ex.ToString();

            Console.WriteLine(error);
            _eventLog.AppendLine(error);

            Console.ForegroundColor = ConsoleColor.Gray;
        }
    }
}
